<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/12/31
 * Time: 2:01
 */

namespace app\common\model;


class Curl
{
    /*xml转数组
     * */
    public static function xmlToArr($xml){
        // 创建解析器
        $parser = xml_parser_create();
        // 将 XML 数据解析到数组中
        xml_parse_into_struct($parser, $xml, $vals, $index);
        // 释放解析器
        xml_parser_free($parser);
        // 数组处理
        $arr = array();
        $t=0;
        foreach($vals as $value) {
            $type = $value['type'];
            $tag = $value['tag'];
            $level = $value['level'];
            $attributes = isset($value['attributes'])?$value['attributes']:"";
            $val = isset($value['value'])?$value['value']:"";
            switch ($type) {
                case 'open':
                    if ($attributes != "" || $val != "") {
                        $arr[$t]['tag'] = $tag;
                        $arr[$t]['attributes'] = $attributes;
                        $arr[$t]['level'] = $level;
                        $t++;
                    }
                    break;
                case "complete":
                    if ($attributes != "" || $val != "") {
                        $arr[$t]['tag'] = $tag;
                        $arr[$t]['attributes'] = $attributes;
                        $arr[$t]['val'] = $val;
                        $arr[$t]['level'] = $level;
                        $t++;
                    }
                    break;
            }
        }
        return $arr;
    }

    /*随机生成ip
     * */
    public static function randIP(){
        $ip2id= round(rand(600000, 2550000) / 10000); //第一种方法，直接生成
        $ip3id= round(rand(600000, 2550000) / 10000);
        $ip4id= round(rand(600000, 2550000) / 10000);
        //下面是第二种方法，在以下数据中随机抽取
        $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
        $randarr= mt_rand(0,count($arr_1)-1);
        $ip1id = $arr_1[$randarr];
        return $ip1id.".".$ip2id.".".$ip3id.".".$ip4id;
    }

    public static function Curl($url,$form=array(),$is_ssl=false,$is_json=false,$timeout=10,$referer=''){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch ,CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        if($form) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form));
        }

        if($is_json==true){
            $data_string =json_encode($form);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data_string))
            );
        }else{
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: 0' )
            );
        }

        if($is_ssl==true){
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }
        if(!empty($referer)){
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      //  curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));
       /* ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();*/
        $result = curl_exec($ch);
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return array('status'=>$return_code,'data'=> $result);
    }
}